package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttn.business.service.CourseUnitHearingDetailsService;
import com.ttn.business.service.CourseUnitHearingService;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitHearing;
import com.ttn.common.core.entity.business.CourseUnitHearingDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (course_unit_hearing_details)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-hearing-details")
@Slf4j
public class CourseUnitHearingDetailsController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitHearingDetailsService courseUnitHearingDetailsService;

    @Resource
    private CourseUnitHearingService courseUnitHearingService;

    @DeleteMapping("/delete/course-unit-hearing-details-by-id/{id}")
    public Result deleteCourseUnitHearingDetailsById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitHearingDetailsController deleteCourseUnitHearingDetailsById ,参数id:{}", id);
        return Result.success(courseUnitHearingDetailsService.removeById(id));
    }

    /**
     * 查看单元题目数
     *
     * @param request
     * @param courseUnitId
     * @return
     */
    @PostMapping("/service/query/course-unit-hearing-details-by-course-unit-id/{courseUnitId}")
    public Result serviceQueryCourseCourseUnitHearingDetailsByCourseUnitId(@PathVariable("courseUnitId") Integer courseUnitId) {


        log.info("CourseUnitHearingDetailsController serviceQueryCourseCourseUnitHearingDetailsByCourseUnitId ,参数courseUnitId:{}", courseUnitId);
        LambdaQueryWrapper<CourseUnitHearing> courseUnitHearingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitHearingLambdaQueryWrapper.eq(CourseUnitHearing::getCourseUnitId, courseUnitId);
        List<CourseUnitHearing> courseUnitHearingList =
                courseUnitHearingService.list(courseUnitHearingLambdaQueryWrapper);
        if (courseUnitHearingList.size() > 0) {
            LambdaQueryWrapper<CourseUnitHearingDetails> courseUnitHearingDetailsLambdaQueryWrapper =
                    new LambdaQueryWrapper<>();
            courseUnitHearingDetailsLambdaQueryWrapper.in(CourseUnitHearingDetails::getCourseUnitHearingId, courseUnitHearingList.stream().map(CourseUnitHearing::getId).collect(Collectors.toList()));
            return Result.success(courseUnitHearingDetailsService.count(courseUnitHearingDetailsLambdaQueryWrapper));
        }

        return Result.success(0);
    }


}
